import {
  setgetList,
  setdelete,
  setsave,
  getParameterSetting,
  saveParameterSetting
} from '@/api/basicConfig/CPYquality.js'
import { permissionsCompanyList } from "@/api/common.js";
export default {
  data() {
    return {
      companylist: [],
      companyId: "",
      formData: {
        type: null,
        testTypeId: null,
        itemsId: null,
        claimId: null,
        //number:'',
      },
      list: [],
      showlist: [],
      symbol: '',
      table: {},
      showtestTypeId: null,
      showitemsId: null,
      showclaimId: null,
      showtype: null,
      loading: false,
      input: '',
      itemList: [],
      dataType: 1,
      oldtypes: [],
      oldirequire: [],
      olditem: [],
      isStrictly: false
    }
  },
  watch: {
    list: {
      handler() {
        if (JSON.stringify(this.showlist) == JSON.stringify(this.list)) {
          this.loading = false
        } else {
          this.loading = true
        }
      },
      deep: true,
      immediate: false
    },
    dataType: {
      handler() {
        let _this = this
        console.log(JSON.parse(JSON.stringify(this.oldtypes)))
        _this.table.types = _this.oldtypes.filter(res => (res.dataType == _this.dataType))
        _this.table.item = _this.olditem.filter(res => (res.dataType == _this.dataType))
        _this.table.require = _this.oldirequire.filter(res => (res.dataType == _this.dataType))
        _this.itemList = JSON.parse(JSON.stringify(_this.table.item))
      },
      deep: true,
      immediate: false
    },
    input() {
      this.table.item = new Array();
      if (this.input == '') {
        this.table.item = this.itemList;
      } else {
        this.itemList.forEach(item => {
          if (item.name.indexOf(this.input) !== -1) {
            this.table.item.push(item);
          }
        });
      }
    },
    companyId() {
      let _this = this;
      _this.init();
    },
  },
  computed: {
    dateRange() {
      const {
        testTypeId,
        itemsId,
        claimId
      } = this.formData
      return {
        testTypeId,
        itemsId,
        claimId
      }
    }
  },
  async created() {
    this.companylist = await permissionsCompanyList()
    this.companyId = this.companylist[0].id
  },
  methods: {
    /*混凝土原物料切换*/
    change1(num) {
      this.dataType = num
    },
    changetype() {
      let _this = this
      if (_this.dateRange.testTypeId !== null && _this.dateRange.itemsId !== null && _this.dateRange.claimId !== null) {
        _this.formData.type = this.showtype
        _this.loading = true
      } else {
        _this.showtype = null
        _this.$message({
          message: '请先选择对应的类别',
          type: 'warning'
        });
      }
    },
    changeitemsId() {
      let _this = this;
      if (_this.loading) {
        _this
          .$confirm('请保存更改数据,否则数据将会丢失?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            cancelButtonClass: 'bottom_all_two',
            confirmButtonClass: 'bottom_all',
            type: 'warning'
          })
          .then(() => {
            _this.showitemsId = _this.formData.itemsId;
            _this.saveSet();
          })
          .catch(() => {
            _this.formData.itemsId = _this.showitemsId;
            _this.getItemlist()
            _this.loading = false;
          });
        return;
      }
      _this.formData.itemsId = _this.showitemsId;
      _this.getItemlist()
    },
    changeclaimId() {
      let _this = this;
      if (_this.loading) {
        _this
          .$confirm('请保存更改数据,否则数据将会丢失?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            cancelButtonClass: 'bottom_all_two',
            confirmButtonClass: 'bottom_all',
            type: 'warning'
          })
          .then(() => {
            _this.showclaimId = _this.formData.claimId;
            _this.saveSet();
          })
          .catch(() => {
            _this.formData.claimId = _this.showclaimId;
            _this.getItemlist()
            _this.loading = false;
          });
        return;
      }
      _this.formData.claimId = _this.showclaimId;
      _this.getItemlist()
    },
    changetestTypeId() {
      let _this = this;
      if (_this.loading) {
        _this
          .$confirm('请保存更改数据,否则数据将会丢失?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            cancelButtonClass: 'bottom_all_two',
            confirmButtonClass: 'bottom_all',
            type: 'warning'
          })
          .then(() => {
            _this.showtestTypeId = _this.formData.testTypeId;
            _this.saveSet();
          })
          .catch(() => {
            _this.formData.testTypeId = _this.showtestTypeId;
            _this.getItemlist()
            _this.loading = false;
          });
        return;
      }
      _this.formData.testTypeId = _this.showtestTypeId;
      _this.getItemlist()
    },
    getItemlist() {
      let _this = this;
      if (_this.dateRange.testTypeId !== null && _this.dateRange.itemsId !== null && _this.dateRange.claimId !== null) {
        getParameterSetting({
          testTypeId: _this.formData.testTypeId, //检测类型id
          itemsId: _this.formData.itemsId, //验证项目id
          claimId: _this.formData.claimId //验证要求id
        }).then((res) => {
          if (res.data) {
            _this.formData.type = res.data.type
            _this.showtype = res.data.type
            _this.isStrictly = res.data.isStrictly == 1 ? true : false
            if (_this.formData.type == 3) {
              _this.symbol = res.data.symbol
            }
            if (_this.formData.type == 1) {
              _this.list = _this.copy(res.data.list)
              _this.showlist = _this.copy(res.data.list)
            }
            return
          }
          _this.showtype = null
          _this.showlist = []
          _this.list = []
          _this.symbol = null
          _this.formData.type = ''
        })
      }
    },
    addlist() {
      let _this = this
      let obj = {
        name: '',
        type: 1
      }
      _this.list.push(obj)
    },
    deletelist(index) {
      this.list.splice(index, 1)
    },
    /*为文本时*/
    saveSet() {
      let _this = this;
      if (_this.dateRange.testTypeId !== null && _this.dateRange.itemsId !== null && _this.dateRange.claimId !== null) {
        if (_this.formData.type == 2) {
          _this.savetItem()
          return
        }
        if (_this.formData.type == 3) {
          _this.formData = {
            ..._this.formData,
            symbol: _this.symbol
          }
          _this.savetItem()
          return
        }
        if (_this.formData.type == 1) {
          let allHasData = _this.list.filter((item) => (
            _this.showlist.some((items) => (item.id == items.id))
          ))

          let deleteData = this.showlist.filter((item) => (
            !allHasData.some((items) => (item.id == items.id))
          ))
          let addData = _this.list.filter((item) => (
            !allHasData.some((items) => (item.id == items.id))
          ))
          allHasData = allHasData.map(res => {
            let obj = new Object()
            obj.name = res.name
            obj.id = res.id
            obj.type = 2
            return obj
          })
          deleteData = deleteData.map(res => {
            let obj = new Object()
            obj.name = res.name
            obj.id = res.id
            obj.parameterSettingId = res.parameterSettingId
            obj.type = 0
            return obj
          })
          _this.formData.companyId = _this.companyId
          _this.formData = {
            ..._this.formData,
            list: [...allHasData, ...deleteData, ...addData]
          }
          _this.savetItem()
          return
        }
      }
    },
    copy(list) {
      let str = JSON.stringify(list)
      return JSON.parse(str)
    },
    savetItem() {
      this.formData.companyId = this.companyId
      this.formData.isStrictly = this.isStrictly == true ? 1 : 0
      saveParameterSetting(this.formData).then((res) => {
        if (res.code == 1001) {
          this.$message({
            message: '恭喜你，保存成功',
            type: 'success'
          });
          this.loading = false;
          this.getItemlist()
        }
      })
    },
    init() {
      setgetList({ companyId: this.companyId }).then(res => {
        this.olditem = res.data.item
        this.oldirequire = res.data.require
        this.oldtypes = res.data.types
        this.table.types = this.oldtypes.filter(res => (res.dataType == this.dataType))
        this.table.item = this.olditem.filter(res => (res.dataType == this.dataType))
        this.itemList = JSON.parse(JSON.stringify(this.table.item))
        this.table.require = this.oldirequire.filter(res => (res.dataType == this.dataType))
        this.$forceUpdate()
      })
    },
    deletetext(low) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'bottom_all_two',
        confirmButtonClass: 'bottom_all',
        type: 'warning'
      }).then(() => {
        setdelete({
          id: low.id
        }).then((res) => {
          if (res.code == 1001) {
            this.init()
          }
        })
      }).catch(() => { });
    },
    addtext(type) {
      this.$prompt('', '输入名称', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'bottom_all_two',
        confirmButtonClass: 'bottom_all',
        inputValidator: (val) => {
          if (val == '' || val == null) {
            return '名称不能为空'
          }
        }
      }).then(({
        value
      }) => {
        let data = {
          name: value,
          type: type,
          dataType: this.dataType
        }
        setsave(data).then(res => {
          if (res.code == 1001) {
            this.init()
          }
        })
      }).catch(() => { });
    },
  }

};